<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */

$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/xrd.php';
require_once $basedir . '/../../../classes/xrd/hostmeta.php';
require_once $basedir . '/../../../classes/xrd/childelement.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/host.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/templatelink.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/templatelink/title.php';

class Xrd_HostMetaTest extends PHPUnit_Framework_TestCase {
    
    public function testCreationOfHostMetaFromRawXml() {
        $rawXml = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>identi.ca</hm:Host>
  <Link rel="lrdd" template="http://identi.ca/main/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
        $obj = Xrd_HostMeta::buildFromRawXML($rawXml);
        $this->assertEquals($rawXml, $obj->asXML());
    }
    
    public function testParsingOfHost() {
        $rawXml = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>identi.ca</hm:Host>
  <Link rel="lrdd" template="http://identi.ca/main/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
        $obj = Xrd_HostMeta::buildFromRawXML($rawXml);
        $this->assertEquals("identi.ca", $obj->getHostValue());
    }
    
    public function testParsingOfTemplate() {
        $rawXml = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>identi.ca</hm:Host>
  <Link rel="lrdd" template="http://identi.ca/main/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
        $obj = Xrd_HostMeta::buildFromRawXML($rawXml);
        $this->assertEquals("identi.ca", $obj->getHostValue());
        $this->assertEquals("http://identi.ca/main/xrd?uri={uri}", $obj->getTemplateValue());
    }
    
    public function testCreationOfHostMetaFromArray() {
       
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>boobs.com</hm:Host>
  <Link rel="lrdd" template="http://boobs.com/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
        $array[Xrd_HostMeta::KEY_HOST] = 'boobs.com';
        $array[Xrd_HostMeta::KEY_TEMPLATE] = 'http://boobs.com/xrd?uri={uri}';
        
        $obj = Xrd_HostMeta::buildFromArray($array);
        $this->assertEquals($expected, $obj->asXML());        
    }
    
// TODO, put this to work    
//    public function testParsingOfVariableTemplate() {
//        $expected = '<?xml version="1.0" encoding="UTF-8" ? >
//<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
//  <hm:Host>lobstermonster.org</hm:Host>
//  <Link rel="lrdd" template="http://localhost/index?controller=lrdd%26uri={uri}">
//    <Title>Resource Descriptor</Title>
//  </Link>
//</XRD>
//';
//        $obj = Xrd_HostMeta::buildFromRawXML($expected);
//        $this->assertEquals($expected, $obj->asXML());
//        $this->assertEquals("http://identi.ca/main/xrd?uri={uri}", $obj->getTemplateValue());
//    }
    
}